package com.derbysoft.dswitch.adapter.ihg.dswitch2.translate;

import com.derbysoft.dswitch.adapter.ihg.common.utils.EnvironmentUtils;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.dto.AvailRequestSegment;
import com.derbysoft.dswitch.adapter.ihg.dto.AvailRequestSegments;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.RoomStayCandidates;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRQ;
import org.springframework.stereotype.Component;

@Component("ds2HotelAvailRQTranslator")
public class HotelAvailRQTranslator extends BaseTranslator
        implements Translator<GenericRQ<HotelAvailRQ>, OTAHotelAvailRQ, Void> {

    @Override
    public OTAHotelAvailRQ translate(GenericRQ<HotelAvailRQ> request, Void aVoid) {
        OTAHotelAvailRQ hotelAvailabilityRequest = new OTAHotelAvailRQ();
        setHeader(hotelAvailabilityRequest, request.getHeader());
        hotelAvailabilityRequest.setAvailRequestSegments(translateAvailRequestSegments(request));
        return hotelAvailabilityRequest;
    }

    private AvailRequestSegments translateAvailRequestSegments(GenericRQ<HotelAvailRQ> request) {
        HotelAvailRQ hotelAvailRQ = request.getPayload();
        AvailRequestSegment availRequestSegment = new AvailRequestSegment();
        availRequestSegment.setAvailReqType(EnvironmentUtils.getIhgAvailabilityRequestType());
        availRequestSegment.setHotelSearchCriteria(createHotelSearchCriteria(hotelAvailRQ.getAvailCriteria().getHotelRef().getCode()));
        availRequestSegment.setStayDateRange(translateStayDateRange(hotelAvailRQ.getAvailCriteria().getStayDateRange()));
        availRequestSegment.setRoomStayCandidates(createRoomStayCandidates(hotelAvailRQ));
        availRequestSegment.setRatePlanCandidates(createRatePlanCandidates(request.getHeader().getSource().getUniqueID().getId()));
        AvailRequestSegments availRequestSegments = new AvailRequestSegments();
        availRequestSegments.getAvailRequestSegment().add(availRequestSegment);
        return availRequestSegments;
    }

    private RoomStayCandidates createRoomStayCandidates(HotelAvailRQ hotelAvailRQ) {
        RoomStayCandidates roomStayCandidates = new RoomStayCandidates();
        roomStayCandidates.getRoomStayCandidate()
                .addAll(translateRoomStayCandidates(hotelAvailRQ.getAvailCriteria().getRoomStayCandidates()));
        return roomStayCandidates;
    }
}
